package com.cy.pj.modules.service.impl;

import com.cy.pj.common.bo.Node;
import com.cy.pj.modules.entity.Category;
import com.cy.pj.modules.mapper.CategoryMapper;
import com.cy.pj.modules.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类目Service业务层处理
 *
 * @author kiven
 * @date 2023-02-09
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询类目
     *
     * @param id 类目ID
     * @return 类目
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询类目列表
     *
     * @param category 类目
     * @return 类目
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }
    @Override
    public List<Category> selectCategoryList1()
    {
        return categoryMapper.selectCategoryList1();
    }
    /**
     * 新增类目
     *
     * @param category 类目
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改类目
     *
     * @param category 类目
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        category.setUpdateTime(new Date());
        return categoryMapper.updateCategory(category);
    }

    /**
     * 删除类目对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(String ids)
    {
        return categoryMapper.deleteCategoryByIds(ids.split(","));
    }

    /**
     * 删除类目信息
     *
     * @param id 类目ID
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    /**
     * 查询类目树列表
     *
     * @return 所有类目信息
     */
    @Override
    public List<Node> selectCategoryTree()
    {
        List<Category> categoryList = categoryMapper.selectCategoryList(new Category());
        List<Node> ztrees = new ArrayList<Node>();
        for (Category category : categoryList)
        {
            Node ztree = new Node();
            ztree.setId(Integer.parseInt(category.getId()+""));
            ztree.setParentId(Integer.parseInt(category.getParentId()+""));
            ztree.setName(category.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
